<template>
  <div class="parking-space-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <el-icon class="is-loading">
        <Loading />
      </el-icon>
      加载中...
    </div>

    <!-- 空状态 -->
    <el-empty v-else-if="tableData.length === 0" description="暂无车位信息" />

    <!-- 车位信息卡片 -->
    <div v-else class="cards-container">
      <div
          v-for="(item, index) in tableData"
          :key="index"
          class="parking-card"
      >

        <!-- 表格内容 -->
        <div class="info-table">
          <!-- 第一行：所属小区 + 楼宇名称 -->
          <div class="table-row">
            <div class="table-cell label-cell">所属小区</div>
            <div class="table-cell value-cell">{{ item.residentialArea || '万象城' }}</div>
            <div class="table-cell label-cell">楼宇名称</div>
            <div class="table-cell value-cell">{{ item.buildingName || '1号楼' }}</div>
          </div>

          <!-- 第二行：车库名称 + 车库区域 -->
          <div class="table-row">
            <div class="table-cell label-cell">车库名称</div>
            <div class="table-cell value-cell">{{ item.garageName || '地下车库' }}</div>
            <div class="table-cell label-cell">车库区域</div>
            <div class="table-cell value-cell">{{ item.areaName || 'A区' }}</div>
          </div>

          <!-- 第三行：车库类别 + 车位面积 -->
          <div class="table-row">
            <div class="table-cell label-cell">车库类别</div>
            <div class="table-cell value-cell">
              <span>{{ item.parkingType || '固定车位' }}</span>
            </div>
            <div class="table-cell label-cell">车位面积</div>
            <div class="table-cell value-cell">{{ item.area ? item.area + '㎡' : '12.5㎡' }}</div>
          </div>

          <!-- 第四行：车位编号 + 备注 -->
          <div class="table-row">
            <div class="table-cell label-cell">车位编号</div>
            <div class="table-cell value-cell">{{ item.parkingNo || 'A-001' }}</div>
            <div class="table-cell label-cell">备注</div>
            <div class="table-cell value-cell">{{ item.description || '地下固定车位，靠近电梯' }}</div>
          </div>

          <!-- 第五行：状态 + 车牌前缀 -->
          <div class="table-row">
            <div class="table-cell label-cell">状态</div>
            <div class="table-cell value-cell">
              <span class="plain-text">{{ item.status || '已分配' }}</span>
            </div>
            <div class="table-cell label-cell">车牌前缀</div>
            <div class="table-cell value-cell">{{ item.prefixLetter || 'A' }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="ParkingSpaceCard">
import { ref, onMounted, watch } from 'vue'
import { Loading } from '@element-plus/icons-vue'

// 定义props
const props = defineProps({
  parkingSpaceData: {
    type: Array,
    default: () => []
  }
})

const tableData = ref([])
const loading = ref(false)

// 车位类型标签样式
const getParkingTypeTagType = (parkingType) => {
  const typeMap = {
    '固定车位': 'success',
    '人防车库': 'warning',
    '临时车位': 'info'
  }
  return typeMap[parkingType] || 'info'
}

// 状态标签类型
const getStatusTagType = (status) => {
  const statusMap = {
    '已分配': 'success',
    '空闲': 'info',
    '维修中': 'warning',
    '已出租': 'primary'
  }
  return statusMap[status] || 'info'
}

// 监听props变化
watch(
    () => props.parkingSpaceData,
    (newData) => {
      tableData.value = newData
    },
    { immediate: true, deep: true }
)

// 初始化数据
onMounted(() => {
  console.log('车位数据:', props.parkingSpaceData)
})
</script>

<style scoped lang="scss">
.parking-space-container {
  padding: 0;
}

.cards-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.parking-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 0;
  margin-bottom: 20px;
  overflow: hidden;
}

.table-title {
  color: white;
  padding: 16px 24px;
  font-size: 16px;
  font-weight: 600;
  border-bottom: 1px solid #ebeef5;
}

.info-table {
  padding: 0;
}

.table-row {
  display: flex;
  border-bottom: 1px solid #f0f0f0;

  &:last-child {
    border-bottom: none;
  }

  &:nth-child(even) {
    background-color: #fafafa;
  }

  &:hover {
    background-color: #f5f7fa;
  }
}

.table-cell {
  padding: 16px 20px;
  font-size: 14px;
  display: flex;
  align-items: center;
  min-height: 52px;
  box-sizing: border-box;

  &.label-cell {
    width: 120px;
    background-color: #f8f9fa;
    color: #606266;
    font-weight: 600;
    border-right: 1px solid #e8e8e8;
    justify-content: flex-start;
  }

  &.value-cell {
    flex: 1;
    color: #303133;
    border-right: 1px solid #f0f0f0;
    min-width: 200px;

    &:last-child {
      border-right: none;
    }
  }
}

.info-tag {
  font-weight: 500;
  border: none;
  padding: 6px 12px;
}

.loading-state {
  text-align: center;
  padding: 60px 0;
  color: #606266;

  .el-icon {
    margin-right: 8px;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .table-row {
    flex-direction: column;
    border-bottom: 2px solid #e8e8e8;
  }

  .table-cell {
    width: 100% !important;
    border-right: none;
    border-bottom: 1px solid #f0f0f0;

    &.label-cell {
      background-color: #f0f2f5;
      font-weight: 600;
    }

    &:last-child {
      border-bottom: none;
    }
  }
}

:deep(.el-tag) {
  margin-right: 0;
  border: none;
}
</style>